package Client.GUI.Popups;

import Client.*;
import Client.Net.*;
import Library.ErrorCodes;
import Library.Events.TimeEvent;
import Library.Game.*;
import java.rmi.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

/**
 *
 */
public class popAttack extends AbstractPopup
{
    protected LandInfo m_AttackingLand;
    protected LandInfo m_DefendingLand;

    /**
     * Creates new form popDecideTurnAction
     */
    public popAttack(ICommunication communication, IPopupContainer container, LandInfo landAttacking, LandInfo landBeingAttacked)
    {
	super(communication, container);
        // bepalen max aantal te gebruiken legers
       // spinmax is maximaal te gebruiken legers spin def is de default waarde van de spinbox
        if (landAttacking.getAmountOfArmies()>4)
        {
          spinmax = 3;
        }
        else
        {
            spinmax = landAttacking.getAmountOfArmies()-1;
        }
        spindef = spinmax;
       	initComponents();
	m_AttackingLand = landAttacking;
	m_DefendingLand = landBeingAttacked;
	String defName = "<unknown>";

	try
	{
	    Color attClr = PlayerColor.getColor(landAttacking.getOwner().getColor());
	    Color defClr = PlayerColor.getColor(landBeingAttacked.getOwner().getColor());
	    lblAttackerWho.setForeground(attClr);
	    lblAttackerLand.setForeground(attClr);
	    lblAttackerArmies.setForeground(attClr);
	    lblDefenderWho.setForeground(defClr);
	    lblDefenderLand.setForeground(defClr);
	    lblDefenderArmies.setForeground(defClr);
	    lblTimeRemaining.setForeground(attClr);
	    lblTimeRemainingText.setForeground(attClr);
	    defName = landBeingAttacked.m_Player.getName();
	}
	catch (RemoteException ex)
	{
	    ex.printStackTrace();
	}
	
	lblAttackerWho.setText("Attacker (You)");
	lblAttackerLand.setText(landAttacking.getName());
	lblAttackerArmies.setText(landAttacking.getAmountOfArmies() + " soldiers stationed");
	lblDefenderWho.setText("Defender " + defName);
	lblDefenderLand.setText(landBeingAttacked.getName());
	lblDefenderArmies.setText(landBeingAttacked.getAmountOfArmies() + " soldiers stationed");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCancel = new javax.swing.JButton();
        btnAttack = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        numAantal = new javax.swing.JSpinner();
        jPanel1 = new javax.swing.JPanel();
        lblAttackerLand = new javax.swing.JLabel();
        lblAttackerArmies = new javax.swing.JLabel();
        lblAttackerWho = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        lblDefenderLand = new javax.swing.JLabel();
        lblDefenderArmies = new javax.swing.JLabel();
        lblDefenderWho = new javax.swing.JLabel();
        lblTimeRemainingText = new javax.swing.JLabel();
        lblTimeRemaining = new javax.swing.JLabel();

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnAttack.setText("Attack");
        btnAttack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAttackActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setText("You are attacking!!");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setText("With how many soldiers do you want to attack ?");

        numAantal.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        numAantal.setModel(new javax.swing.SpinnerNumberModel(spindef, 0, spinmax, 1));

        lblAttackerLand.setText("Land");

        lblAttackerArmies.setText("Armies");

        lblAttackerWho.setText("Who");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAttackerArmies)
                    .addComponent(lblAttackerLand)
                    .addComponent(lblAttackerWho))
                .addContainerGap(129, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(lblAttackerWho)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblAttackerLand)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblAttackerArmies)
                .addContainerGap())
        );

        lblDefenderLand.setText("Land");

        lblDefenderArmies.setText("Armies");

        lblDefenderWho.setText("Who");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDefenderArmies)
                    .addComponent(lblDefenderLand)
                    .addComponent(lblDefenderWho))
                .addContainerGap(144, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(lblDefenderWho)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblDefenderLand)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDefenderArmies)
                .addContainerGap())
        );

        lblTimeRemainingText.setText("Time Remaining (Round):");

        lblTimeRemaining.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        lblTimeRemaining.setText("0 seconds");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnCancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 215, Short.MAX_VALUE)
                        .addComponent(btnAttack))
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(numAantal, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(152, 152, 152)
                                .addComponent(lblTimeRemaining, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblTimeRemainingText)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(numAantal, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblTimeRemainingText)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblTimeRemaining, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnAttack))
                .addGap(26, 26, 26))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAttackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAttackActionPerformed
        try
	{
            
            int amount = ((Integer)numAantal.getValue()).intValue();
            int result = m_Communication.getService().attack(m_AttackingLand.getID(), m_DefendingLand.getID(), amount);
	    if (result != ErrorCodes.ERROR_NOERROR)
		JOptionPane.showMessageDialog(this, "Cannot attack, because: " + ErrorCodes.getMessage(result), "Cannot attack", JOptionPane.ERROR_MESSAGE);
        }
	catch (RemoteException ex)
	{
            ex.printStackTrace();
        }
	catch (Exception ex)
	{
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnAttackActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
	m_Container.disposePopups();
    }//GEN-LAST:event_btnCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAttack;
    private javax.swing.JButton btnCancel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblAttackerArmies;
    private javax.swing.JLabel lblAttackerLand;
    private javax.swing.JLabel lblAttackerWho;
    private javax.swing.JLabel lblDefenderArmies;
    private javax.swing.JLabel lblDefenderLand;
    private javax.swing.JLabel lblDefenderWho;
    private javax.swing.JLabel lblTimeRemaining;
    private javax.swing.JLabel lblTimeRemainingText;
    private javax.swing.JSpinner numAantal;
    // End of variables declaration//GEN-END:variables
        
    
    int spindef;
    int spinmax;
    protected ActionListener onTurnTimerEvent = new ActionListener()
    {
	@Override
	public void actionPerformed(ActionEvent e)
	{
	    try
	    {
		TimeEvent ev = (TimeEvent)e;
		int time = ev.getTime();
		int timetotal = ev.getTotalTime();
		int hrs = time / 3600;
		int temp = time - (3600 * hrs);
		int mins = temp / 60;
		int secs = temp - (mins * 60);
		lblTimeRemaining.setText(String.format("%02d:%02d:%02d", hrs, mins, secs));
	    }
	    catch (Exception ex)
	    {
		ex.printStackTrace();
	    }
	}
    };
    
    @Override
    protected void register()
    {
	m_Communication.getService().onTurnTimerEvent.register(onTurnTimerEvent);
    }
    
    @Override
    protected void unregister()
    {
	m_Communication.getService().onTurnTimerEvent.unregister(onTurnTimerEvent);
    }
}
